#coding=utf-8
'''
Created on 2015年3月25日
@author: 403720
'''
import httplib
import json
import time
ALLtesttimes=0;
ResultOK1=0;
ResultOK2=0;
ResultNG1=0;
ResultNG2=0;

#连接wifi盒子
conn=httplib.HTTPConnection('192.168.1.101')
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/html"}


while(ALLtesttimes<=4):
    ALLtesttimes=ALLtesttimes+1;
    #Upgrade test,发送升级操作
    Upgrade='{"command": "hota-upgrade" , "url":"http://192.168.1.101:80/upgrade/M130/V1/"}'
    conn.request('POST','/api/hota-upgrade',Upgrade,headers)
    response =conn.getresponse()
    #print Upgrade
    print 'update,v1...',response.status
    #print response.reason
    #print response.read()
   # Upgrade test end

    #for Versioncheck test.
    time.sleep(240)
    conn.request('GET', '/api/request-version')
    Versioncheck=conn.getresponse().read()
    print Versioncheck
    Version=json.loads(Versioncheck)

    if Version["map"]==[{u'version': u'0.0.1.8', u'value': u'7620A'}]:
        ResultOK1=ResultOK1+1
     #print Version["map"]
    else:
        ResultNG1=ResultNG1+1
    
    print '>'*50
    #Versioncheck end.
    #Upgrade test
    Upgrade='{"command": "hota-upgrade" , "url":"http://192.168.1.101:80/upgrade/M130/V2/"}'
    conn.request('POST','/api/hota-upgrade',Upgrade,headers)
    response =conn.getresponse()
    #print Upgrade
    print 'update,v2...',response.status
    print response.reason
    print response.read()
    # Upgrade test end

    #for Versioncheck test.
    time.sleep(240)
    
    conn.request('GET', '/api/request-version')
    Versioncheck=conn.getresponse().read()
    print Versioncheck
    Version=json.loads(Versioncheck)
    if Version["map"]==[{u'version': u'0.0.1.9', u'value': u'7620A'}]:
    #print Version["map"]
        ResultOK2=ResultOK2+1
    else:
        ResultNG2=ResultNG2+1
     #Versioncheck end.
    print "All test times are:%d"%(ALLtesttimes*2)
    print "V18 Upgrade OK times:%d"%ResultOK1
    #print ResultNG1
    print "V19 Upgrade OK times:%d"%ResultOK2
    #print ResultNG2
    
conn.close()




